//
//  MyClientController.m
//  FactorySale
//
//  Created by 主事丫环HU on 2020/5/19.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "MyClientController.h"
#import "MyClientCell.h"
#import "AddNewCustomerController.h"// 添加新客户控制器
#import "EditCustomerInfoController.h"// 编辑客户信息控制器
#import "DeliveryRecordController.h"// 发货记录控制器
#import "SelectProductController.h"// 选择产品控制器
#import "MyClientListModel.h"
#import "CommonPopViewController.h"// 删除客户控制器

@interface MyClientController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *listArray;
@end

@implementation MyClientController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self requestCustomerDataList];
}

- (void)setupUI{
    self.title = @"我的客户";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = Main_BackGroundColor;
    
    [self.rightbutton setImage:imgname(@"添加客户") forState:UIControlStateNormal];
    [self.rightbutton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView.rowHeight = 165;
    self.tableView.tableFooterView = [[UIView alloc] init];
}
- (void)requestCustomerDataList{
    
    self.listArray = [NSMutableArray arrayWithCapacity:0];
    [NetWorkTool POST:getCsFactoryUserList param:nil success:^(id dic) {
        self.listArray = [MyClientListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        [self.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
- (void)deleteUserInfo:(MyClientListModel *)model {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"id"] = model.id;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:deleteFactoryUser param:param success:^(id dic) {
        [self.listArray removeObject:model];
        [self.tableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   return self.listArray.count;
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MyClientListModel *model = self.listArray[indexPath.row];
    MyClientCell *cell = [MyClientCell cellWithTableView:tableView];
    cell.model = model;
    
    [cell.editButton addTarget:self action:@selector(editCustomerInformationButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.editButton.tag = indexPath.row;
    
    [cell.deleteButton addTarget:self action:@selector(deleteCustomerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.deleteButton.tag = indexPath.row;
    
    [cell.recordButton addTarget:self action:@selector(deliveryRecordButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.recordButton.tag = indexPath.row;
    
    [cell.sendButton addTarget:self action:@selector(sendButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.sendButton.tag = indexPath.row;
    return cell;
    
}
// 立即发货
- (void)sendButtonClick:(UIButton *)button{
    MyClientListModel *model = self.listArray[button.tag];
    SelectProductController * vc = [[SelectProductController alloc] init];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
// 查看历史发货记录
- (void)deliveryRecordButtonClick:(UIButton *)button{
    MyClientListModel *model = self.listArray[button.tag];
    DeliveryRecordController *vc = [[DeliveryRecordController alloc] init];
    vc.factoryUserId = model.id;
    [self.navigationController pushViewController:vc animated:YES];
    
}
// 编辑客户信息
- (void)editCustomerInformationButtonClick:(UIButton *)button{
    MyClientListModel *model = self.listArray[button.tag];
    EditCustomerInfoController *vc = [[EditCustomerInfoController alloc] init];
    vc.editCustomerSuccessfulClick = ^{
        [self requestCustomerDataList];
    };
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}
// 删除客户
- (void)deleteCustomerButtonClick:(UIButton *)button {
    MyClientListModel *model = self.listArray[button.tag];
    
    kWeakSelf;
    CommonPopViewController *popVC = [CommonPopViewController new];
    popVC.message = @"确定要删除此客户吗？";
    popVC.cancleBtnTitle = @"取消";
    popVC.confirmBtnTitle = @"确定";
    popVC.confirmBtnClick = ^{
        [weakSelf deleteUserInfo:model];
    };
    [self presentViewController:popVC animated:YES completion:nil];
}
// 添加客户
- (void)rightButtonAction:(UIButton *)button {
    
    AddNewCustomerController *vc = [[AddNewCustomerController alloc] init];
    vc.addCustomerSuccessfulClick = ^{
        [self requestCustomerDataList];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
@end
